
var tree;
var oContextMenu;

var oConfigsWithCheckbox={
		headers:[
			{text:"Code", width:"130px", type:"text"},
			{text:"Name", width:"125px"},
			{text:"", width:"20px"}
			],
		schema:['code','name','checked'],
		selectedRowColor:"#B0C4DE",
		checkbox:true,
		compareColIndex:1
};
var oConfigs={
		headers:[
			{text:"Code", width:"130px"},
			{text:"Name", width:"125px"}
			],
		schema:['code','name'],
		selectedRowColor:"#B0C4DE",
		compareColIndex:1
};
buildTree=function(configs) {
	var data={code:"000",name:"Item", checked:true};
	var datas0=[
		{code:"001",name:"Item1",checked:false},
		{code:"002",name:"Item2",checked:true},
		{code:"003",name:"Item3"}
	];
	var datas1=[
		{code:"001001",name:"Item11",checked:false},
		{code:"001002",name:"Item12",checked:true},
		{code:"001003",name:"Item13"}
	];
	
	
	tree=new MZW.widget.TableTree("treeDiv", configs);
		var root = tree.getRoot();
		
		var oNode = new MZW.widget.TableTreeNode(data, root, true);
		tree.draw();
		for(var i=0;i<datas0.length;i++){
			if(i==0){
				var node1=new MZW.widget.TableTreeNode(datas0[i], oNode, true);
					for(var ix=0;ix<datas1.length;ix++){
						var n=new MZW.widget.TableTreeNode(datas1[ix], node1, false);
						n.hasChildNodes=true;
					}
			}else{
				new MZW.widget.TableTreeNode(datas0[i], oNode, true);
			}
		}
		
		root.refresh();
	
		tree.subscribe("rowClick", function(){
			document.getElementById("status").innerHTML=tree.selectedNode.toString();
		});
		tree.subscribe("rowDbClick", function(){
			alert("rowDbClick:"+tree.selectedNode.toString());
		});
		
		tree.oCurrentNode={};
				
		function onTriggerContextMenu(p_oEvent, p_oMenu) {
			function GetTextNodeFromEventTarget(p_oTarget) { //alert(p_oTarget.tagName)
				if(p_oTarget.tagName){
					if (p_oTarget.tagName.toUpperCase() == "TR" && p_oTarget.getAttribute("isTTRow")) {
						var oCurrNode=tree.getNodeByRowElId(p_oTarget.id);
						tree.onRowClick(oCurrNode);
						return oCurrNode;
					} else {
						if (p_oTarget.parentNode) {
							return GetTextNodeFromEventTarget(p_oTarget.parentNode);
						}
					}
				}
			}//End GetTextNodeFromEventTarget
			var oTextNode = GetTextNodeFromEventTarget(this.contextEventTarget);
			if (oTextNode) {
				tree.oCurrentNode = oTextNode;
			} else {
				this.cancel();
			}
		}	
		function deleteIt() {
			alert(tree.oCurrentNode.toString());
		}
		function addSame() {
			alert(tree.oCurrentNode.toString());
		}
		function addChild() {
			alert(tree.oCurrentNode.toString());
		}
		function editIt(){
			alert(tree.oCurrentNode.toString());
		}	
		var itemdatas = [
		  {text:"addSame", onclick:{fn:addSame}}, 
		  {text:"addChild", onclick:{fn:addChild}},
		  {text:"editItem", onclick:{fn:editIt}}, 
		  {text:"deleteItem", onclick:{fn:deleteIt}}];
		var options = {trigger:tree.id, lazyload:true, itemdata:itemdatas};
		if(!oContextMenu)
			oContextMenu = new YAHOO.widget.ContextMenu("mytreecontextmenu", options);
		oContextMenu.triggerContextMenuEvent.subscribe(onTriggerContextMenu, oContextMenu, true);
}//end buildTree
